import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import path from "node:path";
import { createHtmlPlugin } from "vite-plugin-html";
import unocss from "unocss/vite";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const envInfo = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        imports: ["vue", "vue-router"],
        dts: "src/auto-imports.d.ts",
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dts: "src/components.d.ts",
      }),
      createHtmlPlugin({
        minify: false,
        inject: {
          data: {
            title: envInfo.VITE_APP_NAME,
            lang: envInfo.VITE_APP_LANG_DEFAULT,
          },
        },
      }),
      unocss(),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            hack: `true; @import "${path.resolve(__dirname, "./src/assets/styles/theme.less")}";`,
          },
          javascriptEnabled: true,
        },
      },
    },
    server: {
      host: "0.0.0.0",
      proxy: {
        [envInfo.VITE_API_BASE_PATH]: {
          target: envInfo.VITE_API_PROXY_TARGET,
          changeOrigin: true,
          rewrite: (path) => path.replace(envInfo.VITE_API_BASE_PATH, ""),
        },
      },
    },
  };
});
